package com.amtf.datafile3.cfg.fil;

import java.text.DateFormat;
import java.text.Format;
import java.text.NumberFormat;

import com.amtf.datafile3.DataField;
import com.amtf.datafile3.DataFieldType;
import com.amtf.datafile3.cfg.std.StdDataFieldDefinition;
import com.amtf.datafile3.core.fil.parser.AbsFilDataFieldParser;
import com.amtf.datafile3.core.fil.parser.FilDateFieldParser;
import com.amtf.datafile3.core.fil.parser.FilDecimalFieldParser;
import com.amtf.datafile3.core.fil.parser.FilStringFieldParser;
import com.amtf.datafile3.core.fil.render.FilDataFieldRender;
import com.amtf.datafile3.core.std.type.DateFieldType;
import com.amtf.datafile3.core.std.type.DecimalFieldType;
import com.amtf.datafile3.core.std.type.StringFieldType;

public class FilDataFieldDefinition extends StdDataFieldDefinition {

	public FilDataFieldDefinition(String name, DataFieldType type, int columnStartIndex, int length, Format informat, Format outformat) {
		super(name, type, createParser(type, columnStartIndex, length, informat), new FilDataFieldRender(length, outformat));
	}

	@Override
	public FilDataFieldDefinition clone() {
		FilDataFieldDefinition newObj = new FilDataFieldDefinition(name, null, 0, 0, null, null);
		copyTo(newObj);
		return newObj;
	}

	private static AbsFilDataFieldParser<?, ?> createParser(DataFieldType type, int columnStartIndex, int length, Format informat) {
		AbsFilDataFieldParser<? extends DataField, ?> fieldParser = null;
		if (type instanceof StringFieldType) {
			fieldParser = new FilStringFieldParser(columnStartIndex, length);
		} else if (type instanceof DecimalFieldType) {
			fieldParser = new FilDecimalFieldParser(columnStartIndex, length, (NumberFormat) informat);
		} else if (type instanceof DateFieldType) {
			fieldParser = new FilDateFieldParser(columnStartIndex, length, (DateFormat) informat);
			// } else if (type instanceof ObjectFieldType) {
			// fieldParser = new FilStringFieldParser( trimValue);
		} else {
			fieldParser = new FilStringFieldParser(columnStartIndex, length);
		}

		return fieldParser;
	}

	public int getColumnStartIndex() {
		return ((AbsFilDataFieldParser) parser).getColumnStartIndex();
	}

	public void setColumnStartIndex(int columnStartIndex) {
		((AbsFilDataFieldParser) parser).setColumnStartIndex(columnStartIndex);
	}

	public int getLength() {
		return ((AbsFilDataFieldParser) parser).getLength();
	}

	public void setLength(int length) {
		((AbsFilDataFieldParser) parser).setLength(length);
		((FilDataFieldRender) render).setLength(length);
	}

	public Format getOutformat() {
		return ((FilDataFieldRender) render).getFormat();
	}

	public void setOutformat(Format outformat) {
		((FilDataFieldRender) render).setFormat(outformat);
	}

	public Format getInformat() {
		return ((AbsFilDataFieldParser) parser).getFormat();
	}

	public void setInformat(Format informat) {
		((AbsFilDataFieldParser) parser).setFormat(informat);
	}

}
